# determine how to declare and how to mangle the blas/lapack functions
if(MKL_FOUND)
  set(LAPACK_DECLARE_INC formic/lapack/declare_mkl.h)
  set(LAPACK_MANGLE_INC formic/lapack/mangle_mkl.h)
else(MKL_FOUND)
  # use the fortran-C interface mangling determined by cmake
  # this should be correct so long as the same compilers were used to build blas/lapack
  FortranCInterface_HEADER(${FORMIC_BINARY_DIR}/src/formic/lapack/mangle_other.h MACRO_NAMESPACE "FC_")
  set(LAPACK_DECLARE_INC formic/lapack/declare_other.h)
  set(LAPACK_MANGLE_INC formic/lapack/mangle_other.h)
endif(MKL_FOUND)

# create header for declaration of blas/lapack functions
configure_file(declare.h.in ${FORMIC_BINARY_DIR}/src/formic/lapack/declare.h)

# create header for mangling of blas/lapack function names
configure_file(mangle.h.in ${FORMIC_BINARY_DIR}/src/formic/lapack/mangle.h)

# create the library
add_library(formic_lapack interface.cpp)

# make sure that when the library is linked, blas/lapack are linked as well
target_link_libraries(formic_lapack ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})

# add a test of the library
add_executable(test_lapack test_lapack.cpp)
target_link_libraries(test_lapack formic_lapack)
add_test(test_lapack test_lapack)

# add a test that ensures the previous test finished,
# in case a BLAS/LAPACK routine fails but does not signal
# its failure properly
add_executable(test_lapack_finished test_lapack_finished.cpp)
add_test(test_lapack_finished test_lapack_finished)

# how to install the library
install(
        TARGETS
          formic_lapack
        RUNTIME DESTINATION
          bin
        LIBRARY DESTINATION
          lib
        ARCHIVE DESTINATION
          lib
       )
